package com.yunxi.service.localService.impl.otd;

import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.util.DateUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dao.otd.ProducePlanViewMapper;
import com.yunxi.model.otd.ProducePlanView;
import com.yunxi.otd.pojo.VehicleOtdDetailVO;
import com.yunxi.otd.pojo.VehicleOtdVO;
import com.yunxi.service.localService.OrderCustomerService;
import com.yunxi.service.localService.OrderVehicleService;
import com.yunxi.service.localService.otd.OtdOrderSoService;
import com.yunxi.service.localService.otd.ProducePlanService;
import com.yunxi.service.localService.sto.VehicleOfflineService;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * <p>
 * ProducePlanView 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-08-17
 */
@Service
public class ProducePlanServiceImpl extends ServiceImplPlus<ProducePlanViewMapper, ProducePlanView> implements
    ProducePlanService {


    @Autowired
    @Qualifier("otdOrderSoServiceImpl")
    OtdOrderSoService soService;

    @Autowired
    @Qualifier("orderCustomerServiceImpl")
    OrderCustomerService orderCustomerService;

    @Autowired
    @Qualifier("vehicleOfflineServiceImpl")
    VehicleOfflineService offlineService;

    @Autowired
    @Qualifier("orderVehicleServiceImpl")
    OrderVehicleService orderVehicleService;


    /**
     * 查看OTD标尺、计划、实际
     *
     * @param planNo 生成计划号
     * @return ApiResult
     */
    @Override
    public ApiResult queryOtd(String planNo) {
        ////// OTD
        // 查询计划时间
        VehicleOtdVO plan = baseMapper.selectOtdPlanDate(planNo);
        // 查询实际时间
        VehicleOtdVO actual = baseMapper.selectOtdActualDate(planNo);
        List<VehicleOtdDetailVO> otdList = createOtdList(plan, actual);
        return ResponseUtil.ok(otdList);
//        ///// 车辆信息
//        VehicleOffline car = offlineService.selectCarByPlanNo(planNo);
//        result.put("car", car);
//
//        OtdOrderSo orderSo = soService.findOrderSoByPlanNo(planNo);
//        if(!StringUtils.isEmpty(orderSo.getCusOrderNo())){
//            ///// 客户订单
//            LambdaQueryWrapper<OrderCustomer> customerQueryWrapper = new LambdaQueryWrapper<>();
//            customerQueryWrapper.eq(OrderCustomer::getOrderCustomerNo, orderSo.getCusOrderNo());
//            List<OrderCustomer> orderCustomers = orderCustomerService.list(customerQueryWrapper);
//            if (!CollectionUtils.isEmpty(orderCustomers)) {
//                result.put("customer", orderCustomers.get(0));
//            }
//
//            ////// 分销订单
//            LambdaQueryWrapper<OrderVehicle> orderVehicleQueryWrapper = new LambdaQueryWrapper<>();
//            orderVehicleQueryWrapper.eq(OrderVehicle::getCustomOrderNo, orderSo.getCusOrderNo());
//            List<OrderVehicle> orderVehicles = orderVehicleService.list(orderVehicleQueryWrapper);
//            if (!CollectionUtils.isEmpty(orderVehicles)) {
//                result.put("orderVehicle", orderVehicles.get(0));
//            }
//        }


    }

    /**
     * 查看OTD信息
     *
     * @param planNo 生成计划号
     * @return ApiResult
     */
    @Override
    public ApiResult queryOtdInfo(String planNo) {
        return ResponseUtil.ok(baseMapper.selectOtdInfoByPlanNo(planNo));
    }

    /**
     * OTD计划、OTD实际计算差异，并何必成一个VO
     * @param plan
     * @param actual
     * @return
     */
    private List<VehicleOtdDetailVO> createOtdList(VehicleOtdVO plan, VehicleOtdVO actual) {
        // OTD
        List<VehicleOtdDetailVO> otdList = new ArrayList<>();
        // 获取实体类的所有属性，返回Field数组
        Field[] field = VehicleOtdVO.class.getDeclaredFields();
        for (Field value : field) {
            //获取属性的名字
            String name = value.getName();
            //将属性的首字符大写，方便构造get，set方法
            String getName = name.substring(0, 1).toUpperCase() + name.substring(1);
            //获取属性的类型
            String type = value.getGenericType().toString();
            if ("class java.util.Date".equals(type)) {
                try {
                    // 获取计划和实际时间
                    Method m = VehicleOtdVO.class.getMethod("get" + getName);
                    Date planDate = null;
                    Date actualDate = null;
                    if (plan != null) {
                        planDate = (Date) m.invoke(plan);
                    }
                    if (actual != null) {
                        actualDate = (Date) m.invoke(actual);
                    }
                    VehicleOtdDetailVO detailVO = new VehicleOtdDetailVO();
                    detailVO.setPlanDate(planDate);
                    detailVO.setActualDate(actualDate);
                    if (planDate != null && actualDate != null) {
                        detailVO.setDiff(DateUtil.getDateDiffByDay(planDate, actualDate));
                    }
                    detailVO.setNodeCode(name);
                    otdList.add(detailVO);
                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }

            }
        }
        return otdList;
    }


}
